﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChanExt;
using C.Entities;
using System.ComponentModel;

namespace C.Unite
{
    /// <summary>
    /// 基础数据绑定帮助类
    /// </summary>
    public class DataBaseHelper
    {
        #region 下拉绑定
        /// <summary>
        /// 下拉式绑定基础数据
        /// </summary>
        /// <param name="ddl">下拉框控件</param>
        /// <param name="em">类型</param>
        public static void BindDrp(DropDownList ddl, EnumData.DataBaseEunm em)
        {
            string getName = EnumHelper.GetEnumDesc(em.GetType(), em);
            Sys_BaseDataGroup groupN = TbsDbSession.Default.From<Sys_BaseDataGroup>().Where(Sys_BaseDataGroup._.GroupName == getName).ToFirst();
            if (groupN != null)
            {
                List<Sys_BaseData> baseData = TbsDbSession.Default.From<Sys_BaseData>().Where(Sys_BaseData._.GroupID == groupN.ID).OrderBy(Sys_BaseData._.OrderID.Asc, Sys_BaseData._.ID.Desc).ToList();
                ddl.DataSource = baseData;
                ddl.DataTextField = "BaseName";
                ddl.DataValueField = "BaseName";
                ddl.DataBind();
            }
        }
        #endregion

        #region 树绑定
        /// <summary>
        /// 树绑定
        /// </summary>
        /// <param name="tr">树控件</param>
        /// <param name="em">类型</param>
        /// <param name="NavUrl">链接</param>
        public static void BindTree(Tree tr, EnumData.DataBaseEunm em, string NavUrl)
        {
            string getName = EnumHelper.GetEnumDesc(em.GetType(), em);
            Sys_BaseDataGroup groupN = TbsDbSession.Default.From<Sys_BaseDataGroup>().Where(Sys_BaseDataGroup._.GroupName == getName).ToFirst();
            if (groupN != null)
            {

                List<Sys_BaseDataTreeLev> treeLev = TbsDbSession.Default.From<Sys_BaseDataTreeLev>().Where(Sys_BaseDataTreeLev._.GroupID == groupN.ID).ToList();

                List<Sys_BaseDataTreeLev> items = treeLev.FindAll(delegate(Sys_BaseDataTreeLev tas) { return tas.TopID == 0; });// TbsDbSession.Default.From<GS_ArticleSort>().Where(GS_ArticleSort._.FatherID == 0).OrderBy(GS_ArticleSort._.ShowNo.Asc).ToList();
                foreach (Sys_BaseDataTreeLev row in items)
                {
                    TreeNode node = new TreeNode();
                    node.Text = row.NodeName;
                    int c = treeLev.FindAll(p => p.TopID == row.ID).Count;
                    if (c == 0 && NavUrl != "")
                    {
                        node.Target = "main";
                        node.NavigateUrl = NavUrl + "?did=" + row.ID + "&dname=" + row.NodeName;
                    }
                    //else
                    //{
                    //    node.NodeID = row.ID.ToString();
                    //    node.AutoPostBack = true;
                    //    node.EnablePostBack = true;
                    //}
                    node.Expanded = true;
                    tr.Nodes.Add(node);
                    ResolveSubTree(row.ID, node, treeLev, NavUrl);
                }
            }
        }
        private static void ResolveSubTree(int id, TreeNode treeNode, List<Sys_BaseDataTreeLev> treeLev, string url)
        {
            List<Sys_BaseDataTreeLev> items = treeLev.FindAll(delegate(Sys_BaseDataTreeLev tas) { return tas.TopID == id; }); //TbsDbSession.Default.From<GS_ArticleSort>().Where(GS_ArticleSort._.FatherID == id).OrderBy(GS_ArticleSort._.ShowNo.Asc).ToList();
            if (items.Count > 0)
            {
                foreach (Sys_BaseDataTreeLev row in items)
                {
                    TreeNode node = new TreeNode();
                    node.Text = row.NodeName;
                    if (url != "")
                    {
                        node.Target = "main";
                        node.NavigateUrl = url + "?did=" + row.ID + "&dname=" + row.NodeName;
                    }
                    else
                    {
                        node.NodeID = row.ID.ToString();
                        node.AutoPostBack = true;
                        node.EnablePostBack = true;
                    }
                    node.Expanded = true;
                    treeNode.Nodes.Add(node);
                    ResolveSubTree(row.ID, node, treeLev, url);
                }
            }
        }
        #endregion

        #region 例外数据绑定
        /// <summary>
        /// 绑定年份
        /// </summary>
        /// <param name="dll"></param>
        public static void BindYear(DropDownList dll)
        {
            for (int i = 2011; i < DateTime.Now.Year + 3; i++)
            {
                ListItem li = new ListItem();
                li.Text = i.ToString() + "年";
                li.Value = i.ToString();
                dll.Items.Add(li);
            }
        }
        /// <summary>
        /// 绑定月份
        /// </summary>
        /// <param name="dll"></param>
        public static void BindMonth(DropDownList dll)
        {
            for (int i = 1; i < 13; i++)
            {
                ListItem li = new ListItem();
                li.Text = i.ToString() + "月";
                li.Value = i.ToString();
                dll.Items.Add(li);
            }
        }
        /// <summary>
        /// 绑定季度
        /// </summary>
        /// <param name="dll"></param>
        public static void BindSeason(DropDownList dll)
        {
            for (int i = 1; i < 5; i++)
            {
                ListItem li = new ListItem();
                li.Text = "第"+i.ToString()+"季度";
                li.Value = i.ToString();
                dll.Items.Add(li);
            }
        } 
        #endregion
    }
}
